//
//  EAGLView.m
//  DrawColorPolygon
//
//  Created by 畑圭輔 on 10/03/07.
//  Copyright Keisuke Hata 2010. All rights reserved.
//

#import "EAGLView.h"
#import "GLESPrim.h"
#import "ES1Renderer.h"

@implementation EAGLView

@synthesize animating;

@dynamic animationFrameInterval;
@synthesize polyStyle = _polyStyle , lineStyle = _lineStyle;


// You must implement this method
+ (Class) layerClass
{
    return [CAEAGLLayer class];
}

//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id) initWithCoder:(NSCoder*)coder
{    
    if ((self = [super initWithCoder:coder]))
	{
        // Get the layer
        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
        
        eaglLayer.opaque = TRUE;
        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:FALSE],
										kEAGLDrawablePropertyRetainedBacking, 
										kEAGLColorFormatRGBA8, 
										kEAGLDrawablePropertyColorFormat, 
										nil];
		
		// レンダラ作成
		renderer = [[ES1Renderer alloc] init];
		if (!renderer)
		{
			[self release];
			return nil;
		}
       
		// ターゲット追加
		EAGLPoly *tri = [[[EAGLPoly alloc] init] autorelease];
		EAGLLine *line = [[[EAGLLine alloc] init] autorelease];
		
		line.lineWidth = 1;
		[renderer addRenderTarget:tri  targetKey:@"poly"];
		[renderer addRenderTarget:line targetKey:@"line"];
		
		// 各種設定
		animating              = FALSE;
		displayLinkSupported   = FALSE;
		animationFrameInterval = 1;
		displayLink            = nil;
		animationTimer         = nil;
		
		NSString *reqSysVer = @"3.1";
		NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
		if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
			displayLinkSupported = TRUE;
    }
	
    return self;
}

- (void) drawView:(id)sender
{
    [renderer render];
}

- (void) layoutSubviews
{
	[renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
    [self drawView:nil];
}

- (NSInteger) animationFrameInterval
{
	return animationFrameInterval;
}

- (void) setAnimationFrameInterval:(NSInteger)frameInterval
{
	if (frameInterval >= 1)
	{
		animationFrameInterval = frameInterval;
		
		if (animating)
		{
			[self stopAnimation];
			[self startAnimation];
		}
	}
}

- (void) startAnimation
{
	if (!animating)
	{
		if (displayLinkSupported)
		{
			displayLink = [NSClassFromString(@"CADisplayLink") 
						   displayLinkWithTarget:self selector:@selector(drawView:)];
			
			[displayLink setFrameInterval:animationFrameInterval];
			[displayLink addToRunLoop:[NSRunLoop currentRunLoop] 
							  forMode:NSDefaultRunLoopMode];
		}
		else
			animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) 
															target:self 
															selector:@selector(drawView:) 
															userInfo:nil 
															repeats:TRUE];
		
			animating = TRUE;
	}
}

- (void)stopAnimation
{
	if (animating)
	{
		if (displayLinkSupported)
		{
			[displayLink invalidate];
			displayLink = nil;
		}
		else
		{
			[animationTimer invalidate];
			animationTimer = nil;
		}
		
		animating = FALSE;
	}
}

- (void) dealloc
{
    [renderer release];
	
    [super dealloc];
}

- (void) updateAnimation
{
	[self stopAnimation];
	
	// 描画を一旦止めて設定を適用する
	ES1Renderer *rendererObj = renderer;
	EAGLPrim *obj = [rendererObj.renderTargets objectForKey:@"poly"];
	obj.mode = _polyStyle;
	
	obj = [rendererObj.renderTargets objectForKey:@"line"];
	obj.mode = _lineStyle;
	
	[self startAnimation];
}

- (void) updateLineWidth : (int)width
{
	ES1Renderer *rendererObj = renderer;
	EAGLLine *obj = [rendererObj.renderTargets objectForKey:@"line"];
	obj.mode = _lineStyle;
	obj.lineWidth = width;
}

@end
